<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/
	
require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAccessPage extends PHPUnit_Framework_TestCase {

	function test_page_inc_check_access() {
		$access_page = new AccessPage();

		$mock_access = $this->getMock('AccessManager');
		$mock_access->expects($this->once())
				->method('check_access')
				->with("ADMIN");

		$access_page->set_access_manager($mock_access);

		$page = new Page();
		$access_page->page_inc_check_access($page);
	}

	function test_page_inc_execute_action_with_edit_action() {
		$expected_action = "edit";

		$access_page = new AccessPage();

		$mock_alerter  = $this->getMock('Alerter');
		$mock_alerter->expects($this->never())
				->method('add_alert');
		$access_page->set_alerter($mock_alerter);

		$page = new Page();
		$access_page->page_inc_execute_action($page, $expected_action);
	}

	function test_page_inc_execute_action_with_add_action() {
		$expected_action = "add";

		$access_page = new AccessPage();

		$mock_alerter  = $this->getMock('Alerter');
		$mock_alerter->expects($this->never())
				->method('add_alert');
		$access_page->set_alerter($mock_alerter);

		$page = new Page();
		$access_page->page_inc_execute_action($page, $expected_action);
	}

	function test_page_inc_execute_action_with_other_action() {
		$expected_action = "xcvvxcw";

		$access_page = new AccessPage();

		$mock_alerter  = $this->getMock('Alerter');
		$mock_alerter->expects($this->once())
				->method('add_alert')
				->with("unknown action: $expected_action");
		$access_page->set_alerter($mock_alerter);

		$page = new Page();
		$access_page->page_inc_execute_action($page, $expected_action);
	}

	function test_page_inc_execute_not_submitted() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;

		$_POST['submitted'] = $is_submitted;

		$page = new Page();

		$access_page = new AccessPage();

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with('9');
		$access_page->set_error_navigation($mock_error_nav);

		$access_page->page_inc_execute($page);
	}

	function test_page_inc_execute_submitted_and_user_id() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_form = 'expected form';
		$expected_user_id = 12345;

		$_POST['submitted'] = $is_submitted;
		$_POST['user_id'] = $expected_user_id;

		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted_and_guest() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_form = 'expected form';

		$_POST['submitted'] = $is_submitted;
		$_POST['subject'] = "guest";

		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted_and_user_group_id() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_form = 'expected form';
		$expected_user_group_id = 12345;

		$_POST['submitted'] = $is_submitted;
		$_POST['usergroup_id'] = $expected_user_group_id;

		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted_and_ip_address() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_form = 'expected form';
		$expected_ipstring = '127.0.0.1';

		$_POST['submitted'] = $is_submitted;
		$_POST['ipstring'] = $expected_ipstring;

		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted_and_ip_address_and_add_action() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_form = 'expected form';
		$expected_ipstring = '127.0.0.1';
		$expected_form_content = 'expected form content';
		$expected_action_id = 12345;
		$expected_dir_id = 23456;
		$expected_rank = 5;

		$expected_rule = "DENY $expected_action_id in dir #$expected_dir_id for IP '$expected_ipstring' (rank $expected_rank)";

		$_POST['submitted'] = $is_submitted;
		$_POST['ipstring'] = $expected_ipstring;
		$_POST['action_id'] = "$expected_action_id";
		$_POST['dir_id'] = "$expected_dir_id";
		$_POST['rank'] = "$expected_rank";
		$_GET['action'] = 'add';

		$page = new Page();

		$access_page = new AccessPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
					array(
							array(), 
							"ACCESS_RULE.*, MEMBER.name, ACCESS_ACTION.action, USERGROUP.description"), 
					array(
							"((ACCESS_RULE LEFT JOIN ACCESS_ACTION ON ACCESS_RULE.action_id = ACCESS_ACTION.action_id) LEFT JOIN MEMBER ON ACCESS_RULE.user_id = MEMBER.user_id) LEFT JOIN USERGROUP ON ACCESS_RULE.usergroup_id = USERGROUP.usergroup_id"), 
					1, 
					"action ASC, rank ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$access_page->set_db_accessor($mock_db_access);

		$mock_toolbox = $this->getMock('Toolbox'); 
		$mock_toolbox->expects($this->once())
				->method('add_action')
				->with($page, 'add_access', 'url-action', 'add');
		$access_page->set_toolbox($mock_toolbox);

		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->once())
				->method('confirm')
				->with("Bent u zeker dat u deze regel wil toevoegen: '$expected_rule'")
				->will($this->returnValue($expected_form_content));
		$access_page->set_form_creator($mock_creator);

		$access_page->page_inc_execute($page);

		$this->assertEquals($expected_form_content, $page->content['form']);
		$this->assertEquals($expected_headers, $page->content['actable']['headers']); 
		$this->assertEquals($expected_widths, $page->content['actable']['widths']); 
		$this->assertEquals($expected_aligns, $page->content['actable']['aligns']); 
		$this->assertEquals(array(), $page->content['actable']['rows']); 
	}

	function test_page_inc_execute_submitted_and_rule() {
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		//TODO: check what headers, aligns and widths are for and whether they are still relevant for the current setup
		$expected_headers = array('rule', 'action', 'directory', 'subject', 'rank', ' ');
		$expected_aligns = array('left', 'left', 'left', 'left', 'center', 'left');
		$expected_widths = array(70, 100, 100, 300, 30, "*");
		$is_submitted = true;
		$expected_rule = true;

		$_POST['submitted'] = $is_submitted;
		$_POST['rule'] = $expected_rule;

		$page = new Page();

		$access_page = new AccessPage();

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with('9');
		$access_page->set_error_navigation($mock_error_nav);

		$access_page->page_inc_execute($page);
	}
}

?>
